package ${package.Controller};

import ${package.Entity}.${entity};
import ${package.Entity}.request.${entity}PageReq;
import ${package.Entity}.request.${entity}ListReq;
import ${package.Entity}.request.${entity}DetailReq;
<#-- 检查主键字段数量，用于判断是单主键还是联合主键 -->
<#assign primaryKeyCount = 0>
<#if table?? && table.fields??>
<#list table.fields as field>
    <#if field.keyFlag?? && field.keyFlag>
        <#assign primaryKeyCount = primaryKeyCount + 1>
    </#if>
</#list>
</#if>
<#-- 仅为单主键表导入批量删除请求类 -->
<#if primaryKeyCount == 1 || !(table?? && table.fields??)>
import ${package.Entity}.request.BatchDelete${entity}Req;
</#if>
import ${package.Service}.${table.serviceName};
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnpc.ymz.bc.nfinmgmt.constant.IPageResult;
import com.cnpc.ymz.bc.nfinmgmt.constant.Result;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * ${table.comment!} 控制器
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@RestController
@RequestMapping("/${entity?uncap_first}")
@Api(tags = "${table.comment!}相关接口")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    /**
     * ${table.comment!} 服务
     */
    @Resource
    private ${table.serviceName} ${table.serviceName?uncap_first};

    /**
     * 分页查询${table.comment!}列表
     *
     * @param req 查询条件
     * @return ${table.comment!}分页数据
     */
    @ApiOperation(value = "分页查询${table.comment!}列表")
    @PostMapping("/find${entity}ByPage")
    public IPageResult<${entity}> find${entity}ByPage(@RequestBody Find${entity}ByPageReq req) {
        log.info("分页查询${table.comment!}列表: pageNo={}, pageSize={}", req.getPageNo(), req.getPageSize());
        Page<${entity}> result = ${table.serviceName?uncap_first}.find${entity}ByPage(req);
        return new IPageResult<>(result);
    }

    /**
     * 按条件查询${table.comment!}
     *
     * @param req 查询条件
     * @return ${table.comment!}列表数据
     */
    @ApiOperation(value ="查询${table.comment!}列表")
    @PostMapping("/find${entity}ListByCondition")
    public Result<List<${entity}>> find${entity}ListByCondition(@RequestBody Find${entity}ListReq req) {
        log.info("查询${table.comment!}列表");
        List<${entity}> list = ${table.serviceName?uncap_first}.find${entity}ListByCondition(req);
        return Result.success(list);
    }

    /**
     * 新增${table.comment!}
     *
     * @param ${entity?uncap_first} ${table.comment!}对象
     */
    @ApiOperation(value = "新增${table.comment!}")
    @PostMapping("/add${entity}")
    public Result<Void> add${entity}(@RequestBody ${entity} ${entity?uncap_first}) {
        log.info("新增${table.comment!}: {}", ${entity?uncap_first});
        try {
            ${table.serviceName?uncap_first}.add${entity}(${entity?uncap_first});
            return Result.success();
        } catch (Exception e) {
            log.error("新增${table.comment!}失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改${table.comment!}
     *
     * @param ${entity?uncap_first} ${table.comment!}对象
     */
    @ApiOperation(value = "修改${table.comment!}")
    @PostMapping("/mod${entity}")
    public Result<Void> mod${entity}(@RequestBody ${entity} ${entity?uncap_first}) {
        log.info("修改${table.comment!}: {}", ${entity?uncap_first});
        try {
            ${table.serviceName?uncap_first}.mod${entity}(${entity?uncap_first});
            return Result.success();
        } catch (Exception e) {
            log.error("修改${table.comment!}失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除${table.comment!}
     *
     * @param req 删除${table.comment!}的条件
     */
    @ApiOperation(value = "删除${table.comment!}")
    @PostMapping("/del${entity}ById")
    public Result<Void> del${entity}ById(@RequestBody Del${entity}Req req) {
        <#if table?? && table.fields??>
        <#-- 检查主键字段数量 -->
        <#assign primaryKeyCount = 0>
        <#list table.fields as field>
            <#if field.keyFlag?? && field.keyFlag>
                <#assign primaryKeyCount = primaryKeyCount + 1>
            </#if>
        </#list>
        <#if primaryKeyCount == 1>
        <#-- 单一主键删除 -->
        <#list table.fields as field>
        <#if field.propertyName?? && field.keyFlag?? && field.keyFlag>
        log.info("删除${table.comment!}: ${field.propertyName}={}", req.get${field.capitalName}());
        </#if>
        </#list>
        <#else>
        <#-- 联合主键删除 - 记录所有主键字段 -->
        StringBuilder keyInfo = new StringBuilder();
        <#list table.fields as field>
        <#if field.propertyName?? && field.keyFlag?? && field.keyFlag>
        keyInfo.append(keyInfo.length() > 0 ? ", " : "")
               .append("${field.comment!''}: ")
               .append(req.get${field.capitalName}());
        </#if>
        </#list>
        log.info("删除${table.comment!}，联合主键: [{}]", keyInfo);
        </#if>
        try {
            ${table.serviceName?uncap_first}.del${entity}ById(req);
            return Result.success();
        } catch (Exception e) {
            log.error("删除${table.comment!}失败", e);
            return Result.error(e.getMessage());
        }
        <#else>
        log.info("删除${table.comment!}: id={}", req.getId());
        try {
            ${table.serviceName?uncap_first}.del${entity}ById(req);
            return Result.success();
        } catch (Exception e) {
            log.error("删除${table.comment!}失败", e);
            return Result.error(e.getMessage());
        }
        </#if>
    }

    /**
     * 按条件删除${table.comment!}
     *
     * @param conditionMap 删除${table.comment!}的条件
     */
    @ApiOperation("按条件删除${table.comment!}")
    @PostMapping("/del${entity}ByCondition")
    public Result<Integer> del${entity}ByCondition(@RequestBody Map<String, Object> conditionMap) {
        try {
            if (ObjectUtil.isEmpty(conditionMap)) {
                return Result.error("删除条件不能为空");
            }
            final int count = ${table.serviceName?uncap_first}.del${entity}ByCondition(conditionMap);
            return Result.success(count);
        } catch (Exception e) {
            log.error("按条件删除${table.comment!}失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量删除${table.comment!}
     *
     * @param req 删除条件
     */
    <#-- 检查主键字段数量，用于判断是单主键还是联合主键 -->
    <#assign primaryKeyCount = 0>
    <#if table?? && table.fields??>
    <#list table.fields as field>
        <#if field.keyFlag?? && field.keyFlag>
            <#assign primaryKeyCount = primaryKeyCount + 1>
        </#if>
    </#list>
    </#if>
    <#-- 仅为单主键表生成批量删除方法 -->
    <#if primaryKeyCount == 1 || !(table?? && table.fields??)>
    @ApiOperation(value = "批量删除${table.comment!}")
    @PostMapping("/batchDelete")
    public Result<Void> batchDelete${entity}(@RequestBody BatchDelete${entity}Req req) {
        <#if table?? && table.fields??>
        <#list table.fields as field>
        <#if field.propertyName?? && field.keyFlag?? && field.keyFlag>
        log.info("批量删除${table.comment!}: ${field.propertyName}s={}", req.get${field.capitalName}s());
        try {
            ${table.serviceName?uncap_first}.batchDelete${entity}(req);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除${table.comment!}失败", e);
            return Result.error(e.getMessage());
        }
        </#if>
        </#list>
        <#else>
        log.info("批量删除${table.comment!}: ids={}", JsonUtil.toJson(req));
        try {
            ${table.serviceName?uncap_first}.batchDelete${entity}(req);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除${table.comment!}失败", e);
            return Result.error(e.getMessage());
        }
        </#if>
    }
    </#if>
}
</#if> 